package me.luger.core.commom.util;

import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/23 0023.
 */
public class SpelUtil {

    public static String parseKey2String(String key, String[] names, Object[] values) throws Exception {

        if(names.length !=  values.length){
            throw new Exception("names.length not equal values.length");
        }
        //使用SPEL进行key的解析
        ExpressionParser parser = new SpelExpressionParser();
        //SPEL上下文
        StandardEvaluationContext context = new StandardEvaluationContext();
        //把方法参数放入SPEL上下文中
        for (int i = 0; i < names.length; i++) {
            context.setVariable(names[i], values[i]);
        }
        return parser.parseExpression(key,new TemplateParserContext()).getValue(context, String.class);
    }

    public static String parseKey2String(String key, Map<String,Object> variableMap) throws Exception {
        //使用SPEL进行key的解析
        ExpressionParser parser = new SpelExpressionParser();
        //SPEL上下文
        StandardEvaluationContext context = new StandardEvaluationContext();
        //把方法参数放入SPEL上下文中
        context.setVariables(variableMap);
        return parser.parseExpression(key,new TemplateParserContext()).getValue(context, String.class);
    }

    public static Boolean parseKey2Boolean(String key, String[] names, Object[] values) throws Exception {

        if(names.length !=  values.length){
            throw new Exception("names.length not equal values.length");
        }
        //使用SPEL进行key的解析
        ExpressionParser parser = new SpelExpressionParser();
        //SPEL上下文
        StandardEvaluationContext context = new StandardEvaluationContext();
        //把方法参数放入SPEL上下文中
        for (int i = 0; i < names.length; i++) {
            context.setVariable(names[i], values[i]);
        }
        return parser.parseExpression(key).getValue(context, Boolean.class);
    }

    public static Boolean parseKey2Boolean(String key, Map<String,Object> variableMap) throws Exception {
        //使用SPEL进行key的解析
        ExpressionParser parser = new SpelExpressionParser();
        //SPEL上下文
        StandardEvaluationContext context = new StandardEvaluationContext();
        //把方法参数放入SPEL上下文中
        context.setVariables(variableMap);
        return parser.parseExpression(key).getValue(context, Boolean.class);
    }

    public static void main(String[] args){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("taskId","113");
        map.put("test","luger");

        String expression = "#{#map.test}-#{#map.test}-#{#int}";
//        System.out.println(parseKey2String(expression,map));

    }
}
